package com.jt.ktdemo.ui.mian

import android.os.Bundle
import android.os.Message
import android.util.Log
import android.view.View
import android.view.Window
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.jt.ktdemo.R
import com.jt.ktdemo.utils.LogUtil

class MainActivity : AppCompatActivity(), View.OnClickListener {

    val age: Int = 3
    val school: String = "小学"
    private lateinit var detectionFragment: DetectionFragment
    private lateinit var messageFragment:MessageFragment
    private lateinit var myFragment : MyFragment
    private lateinit var fManager: FragmentManager
    private lateinit var tvModuleDetection : TextView
    private lateinit var tvModuleMessage : TextView
    private lateinit var tvModuleMy : TextView
    private lateinit var ivModuleHeart : ImageView
    private lateinit var ivModuleMessage : ImageView
    private lateinit var ivModuleMe : ImageView



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        fManager = supportFragmentManager
        val flModule = findViewById<FrameLayout>(R.id.flModule)
        val llModuleMy = findViewById<LinearLayout>(R.id.llModuleMy)
        val llModuleDetection = findViewById<LinearLayout>(R.id.llModuleDetection)
        val llModuleMessage = findViewById<LinearLayout>(R.id.llModuleMessage)

        ivModuleHeart = findViewById<ImageView>(R.id.imageView5)
        ivModuleMessage = findViewById<ImageView>(R.id.imageView6)
        ivModuleMe = findViewById<ImageView>(R.id.imageView7)

        tvModuleDetection = findViewById(R.id.textView15)
        tvModuleMessage = findViewById(R.id.textView16)
        tvModuleMy = findViewById(R.id.textView17)

        llModuleMy.setOnClickListener(this)
        llModuleDetection.setOnClickListener(this)
        llModuleMessage.setOnClickListener(this)

        detectionFragment = DetectionFragment()
        messageFragment = MessageFragment()
        myFragment = MyFragment()

        switchFragment(detectionFragment)
        LogUtil.e("检测")
        setColorMethod(tvModuleDetection)
        ivModuleHeart.setImageResource(R.drawable.icon_module_first_select)
        //range表达式
        //        if (age in 0..3){
        //            tvModuleHW.setText("婴幼儿")
        //        }else if(age in 3..12){
        //            tvModuleHW.setText("少儿")
        //        }else {
        //            tvModuleHW.setText("青少年")
        //        }

        //when表达式
        //        val leve = when(school){
        //
        //            "幼儿园" -> "婴儿"
        //            "小学" -> "少儿"
        //            "中学" -> "少年"
        //            else -> {
        //                "未知"
        //            }
        //        }
        //
        //        tvModuleHW.setText(leve)

        //        //String模板
        //        val a: String = "jack"
        //        val b: String = "ross"
        //        tvModuleHW.setText("$a love $b")
        //
        //        //String模板三元运算符
        //        val c: Boolean = false
        //        tvModuleHW.setText("牺牲的是：${if (c) "rose" else "jack"}")

        //函数头
        //tvModuleHW.setText(smokingMethod("a"))

        //匿名函数
        //        val total: Int = "miss".count()
        //        tvModuleHW.setText("miss单词位数 $total 个")
        //
        //        val totals : Int = "missission".count { letter ->
        //            letter == 's'
        //        }
        //        tvModuleHW.setText("missission中包含s $totals 个")

        //函数类型 与隐士返回,默认返回最后一行的结果，不需要添加return
        //        val jack:()->String = {
        //            val tom = "fuck"
        //            "$tom you"
        //        }

        //匿名函数的类型推断
        //        val funcation:(String ,Int)->String={name,age->
        //            val tom : String = "19"
        //            "$name 年$age 月$tom 日"
        //        }
        //        tvModuleHW.setText(funcation("2021",11))

        //一个函数作为另一个函数的参数显示
        //        val first : (String ,Int)->String = {year:String,month:Int ->
        //            val day : String = "19"
        //            "$year 年$month 月$day 日，周五"
        //        }
        //
        //        tvModuleHW.setText(orderDay("2021",first))

        //kotlin的可空
        //        var xiaoma:String? = "潇洒"
        //        xiaoma = null
        //        tvModuleHW.setText(xiaoma?.capitalize())

        //let
        //        var xm : String ? = "beautiful"
        //        var xs : String ? = ""
        //        xs = xm?.let {
        //            if (it.isNotBlank()){
        //                it.capitalize()
        //            }else{
        //                "qiankuan"
        //            }
        //        }
        //        tvModuleHW.setText(xs)
        //
        //        //非空断言操作符
        //        tvModuleHW.setText(xm!!.capitalize())
        //
        //        //链式调用
        //        tvModuleHW.setText(xm?.capitalize()?.plus("isGreat")!!.replace("is",""))

        //空合并
        //        var xm : String ? = "bean"
        //        //xm = null
        //        //tvModuleHW.setText(xm ?: "beautiful")
        //        xm = xm?.let { it.capitalize() } ?: "beautiful"
        //        tvModuleHW.setText(xm)
    }

    private fun orderDay(year: String, first: (String, Int) -> String): String {
        val month: Int = (1..12).shuffled().last();
        return "${first(year, month)}"
    }

    private fun smokingMethod(fire: String, yan: String = "c"): String {
        return "$fire $yan"
    }

    override fun onClick(v: View?) {
        initTextView();
        if (v?.id == R.id.llModuleDetection){
            switchFragment(detectionFragment)
            LogUtil.e("检测")
            setColorMethod(tvModuleDetection)
            initBottomRes()
            ivModuleHeart.setImageResource(R.drawable.icon_module_first_select)
        }else if (v?.id == R.id.llModuleMessage){
            LogUtil.e("消息")
            switchFragment(messageFragment)
            setColorMethod(tvModuleMessage)
            initBottomRes()
            ivModuleMessage.setImageResource(R.drawable.icon_module_message_select)
        }else if (v?.id == R.id.llModuleMy){
            LogUtil.e("我的")
            switchFragment(myFragment)
            setColorMethod(tvModuleMy)
            initBottomRes()
            ivModuleMe.setImageResource(R.drawable.icon_module_me_select)
        }
    }

    /**
     * 初始化底部引导按钮字体颜色
     */
    public fun initTextView(){
        tvModuleDetection.setTextColor(ContextCompat.getColor(this,R.color.black))
        tvModuleMessage.setTextColor(ContextCompat.getColor(this,R.color.black))
        tvModuleMy.setTextColor(ContextCompat.getColor(this,R.color.black))
    }

    /**
     * textView 赋值color
     */
    public fun setColorMethod(text:TextView){
        text.setTextColor(ContextCompat.getColor(this,R.color.blue_color))
    }

    public fun initBottomRes(){
        ivModuleHeart.setImageResource(R.drawable.icon_module_first)
        ivModuleMessage.setImageResource(R.drawable.icon_module_message)
        ivModuleMe.setImageResource(R.drawable.icon_module_me)
    }

    /**
     * 切换fragment
     */
    private fun switchFragment(fragment: Fragment) {
        val beginTransaction = fManager.beginTransaction()
        beginTransaction.replace(R.id.flModule, fragment)
        beginTransaction.commit()
    }

}
